package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;


public class CartPage extends BasePage {

    /*---------------locators---------------------------------------------------------------*/
    private static final By formButton = By.id("popup-checkout");
    private static final By quantityField = By.name("quantity");

    /*---------------constructor with dynamic url-------------------------------------------*/
    public CartPage(WebDriver driver, String url)
    {
        super(driver,url);
    }

    /*---------------methods---------------------------------------------------------------*/
    public void open()
    {
        driver.get(pageUrl);

    }

    public void clickOrderBtn()
    {
        driver.findElement(formButton).click();

        // switch focus of WebDriver to the next found window handle (that's your newly opened window)
        for (String winHandle : driver.getWindowHandles()) {
            driver.switchTo().window(winHandle);
        }

        (new WebDriverWait(driver, 20))
                .until(ExpectedConditions.presenceOfElementLocated(By.className("check-title")));

    }

    public void changeQuantityInBasket(String quantity)
    {

        List<WebElement> quantityFields = driver.findElements(quantityField);
        for (WebElement quantityField : quantityFields) {
            quantityField.clear();
            quantityField.sendKeys(quantity);
            driver.findElement(By.cssSelector("a[name=recount]")).click();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }



    }

    public Boolean isBasketEmpty() {
        (new WebDriverWait(driver, 10))
                .until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#drop-block.empty")));

        return driver.findElements(By.cssSelector("#drop-block.empty")).size()!=0;

    }






}
